﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DAL;

namespace UI.Admin
{
    public partial class donhang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HienThiDanhSach();
        }
        void HienThiDanhSach()
        {
            // Get All Sản Phẩm
            DataTable Manufacturers = DBConnection.getALL("getAllDonHang");
            GridView1.DataSource = Manufacturers;
            GridView1.DataBind();
            DataTable dt1 = DBConnection.getALL("sp_getAllPTThanhToan");
            cpptthanhtoan.DataSource = dt1;
            cpptthanhtoan.DataValueField = "MaPT";
            cpptthanhtoan.DataTextField = "TenPT";

            cpptthanhtoan.DataBind();
            DataTable dt2 = DBConnection.getALL("sp_getAllHinhThucGiaoHang");
            cphtgiaohang.DataSource = dt2;
            cphtgiaohang.DataValueField = "MaHT";
            cphtgiaohang.DataTextField = "TenHT";
            cphtgiaohang.DataBind();
            MultiView1.ActiveViewIndex = 0;
        }


        void HienThiKetQuaSua(String manxb)
        {

            SqlParameter[] thamso = new SqlParameter[]
            {
                 new SqlParameter("@MaDH",int.Parse(manxb))
            };
            DataTable dtsp = DBConnection.GetDatatableByStoreProcedureName("sp_getAllDonHangbyMaHD", thamso);
            cpptthanhtoan.Text = dtsp.Rows[0]["MaPT_ThanhToan"].ToString();
            cphtgiaohang.Text = dtsp.Rows[0]["MaHT_GiaoHang"].ToString();
            if (dtsp.Rows[0]["DaGiao"].ToString() == "False")
            {
                chkdagiao.Checked = false;
            }
            else
                chkdagiao.Checked = true;

            SqlParameter[] thamso1 = new SqlParameter[]
            {
                 new SqlParameter("@MaDH",int.Parse(manxb))
            };
            DataTable dt = DBConnection.GetDatatableByStoreProcedureName("TongTienHoaDonbyMaDH", thamso1);
            int tongtien = int.Parse(dt.Rows[0]["tongtien"].ToString());
            SqlParameter[] thamso2 = new SqlParameter[]
            {
                 new SqlParameter("@MaHT",cphtgiaohang.SelectedValue)
            };
            DataTable dt2 = DBConnection.GetDatatableByStoreProcedureName("gethinhthucgiaohangbyMaHT", thamso2);
            int tongtien1 = int.Parse(dt2.Rows[0]["MucPhi"].ToString());

            SqlParameter[] thamso3 = new SqlParameter[]
            {
                 new SqlParameter("@MaDH",int.Parse(manxb))
            };
            DataTable dt3 = DBConnection.GetDatatableByStoreProcedureName("getChiTietDonHang", thamso3);
            GridView2.DataSource = dt3;
            GridView2.DataBind();

            lbtongtien.Text = (tongtien + tongtien1).ToString(); 
            MultiView1.ActiveViewIndex = 1;
        }


        protected void btntmkiem_Click(object sender, EventArgs e)
        {

            //string tennxb = txttim.Text;
            //SqlParameter[] thamso = new SqlParameter[]{
            //new SqlParameter("p_tennhaxb",tennxb)
            //};

            //// search NhaXB by TenNhaXB
            //DataTable dt = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_SearchNhaXuatBanByTenNhaXB", thamso);
            //GridView1.DataSource = dt;
            //GridView1.DataBind();
            //txttim.Text = "";
        }


        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.HienThiDanhSach();

        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnupdate.Visible = true;

            string masp = GridView1.SelectedRow.Cells[2].Text;
            this.HienThiKetQuaSua(masp);
            lbmadh.Text = masp;
        }
        protected void btnback_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }


        protected void btnupdate_Click(object sender, EventArgs e)
        {

            SqlParameter[] thamso1 = new SqlParameter[]
            {
            new SqlParameter("@MaDH",int.Parse(lbmadh.Text)),

             };

            DBConnection.QueryByStoreProcedureName("updateDonHang", thamso1);
            this.HienThiDanhSach();
        }




        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            string maNSX = GridView1.Rows[e.RowIndex].Cells[2].Text;

            try
            {
                SqlParameter[] thamso1 = new SqlParameter[]
                 {
                new SqlParameter("@MaDH",int.Parse(maNSX)),
                 };
                DBConnection.QueryByStoreProcedureName("deleteChiTietDonHang", thamso1);
                SqlParameter[] thamso = new SqlParameter[]
                 {
                new SqlParameter("@MaDH",int.Parse(maNSX)),
                 };
                DBConnection.QueryByStoreProcedureName("deleteDonHang", thamso);
            }
            catch
            {
                System.Console.WriteLine("khong xoa duoc do rang buoc khoa");
            }
            HienThiDanhSach();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }
    }
}